/** Class Name: OORTelesalesUpdateLeadOwner
 * Author: Accenture
 * Date:  20th March 2012
 * Requirement/Project Name: OOR Telesales
 * Requirement Description:   Update owner of Lead under a Campiagn to OOR Telesales Campaign Lead Queue
 **/
 
global Class OORTelesalesUpdateLeadOwner
{
   /**
    *  update the lead owner associated with the Campaign
    *
    */
    WebService static void updateOwner(String CampaignId)
    {
        final ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Lead', OORTelesalesTextConstant.LOCAL_ADVERTISER_LEAD_REC_TYPE);            
        final ID campaginLeadQueue = OORTelesalesUtility.getQueueID(OORTelesalesTextConstant.OOR_TELE_CAMPAIGN_LEAD_QUEUE); 
        final List<Campaign> campaignList = [Select StartDate,Original_Start_Date__c,IsActive , Campaign_Activation_Date__c from Campaign  where id = :CampaignId];
        final List<Lead> leadList = new List<Lead>();
        
        try
        {   
            // check if any record is present        
            if(campaignList.size() > 0)
            {
                // Check if original Start date is present. 
                if(campaignList.get(0).Original_Start_Date__c == null)
                {
                    campaignList.get(0).Original_Start_Date__c = Date.Today();
                }
                campaignList.get(0).StartDate=Date.Today();
                campaignList.get(0).IsActive=True;
                campaignList.get(0).Campaign_Activation_Date__c=Date.Today();
                update campaignList;
            }
                
            for(Lead leadNew: [Select Id,OwnerId from Lead where OORTelesalesCampaign__c=:CampaignId and  Lead_Status_ReadOnly__c=:OORTelesalesTextConstant.LEAD_NEW_STATUS and RecordTypeId=:localAdvertiserRecordTypeID])
            {
                leadNew.ownerId = campaginLeadQueue;                 
                leadList.add(leadNew); 
            }
                
            // Check any record  need to be updated.
            if(leadList.size() > 0 )
            {
                update leadList; 
            }
        }
        catch(Exception ex)
        {
            GeneralConstants.mailAnyExceptionToMe('OORTelesalesUpdateLeadOwner','updateOwner',ex);
        }         
    }
}